package com.yungu.swift.admin.route.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.system.sys.model.dto.SysDepartLimitDto;
import com.yungu.swift.system.sys.model.param.AdminDepartLimitPageParam;
import com.yungu.swift.system.sys.model.vo.SysDepartLimitVo;
import com.yungu.swift.system.sys.service.SysDepartLimitService;
import com.yungu.swift.system.sys.service.SysWayService;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/admin/v1/system/departLimit")
@Api(value = "【系统服务-业务设置中心】", tags = "出发时间限制")
public class DepartLimitAdmin {

    @Reference
    private SysDepartLimitService sysDepartLimitService;

    @Reference
    private SysWayService sysWayService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "分页查询", httpMethod = "POST")
    public ResponseData<PageVo<SysDepartLimitVo>> queryPageVo(@RequestBody AdminDepartLimitPageParam departLimitPageParam) {
        return sysDepartLimitService.queryPageVo(departLimitPageParam);
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增或更新", httpMethod = "POST")
    public ResponseData<Boolean> save(@RequestBody AdminDepartLimitPageParam departLimitPageParam) {
        SysDepartLimitDto systemDepartLimitDto = new SysDepartLimitDto();
        BeanUtils.copyProperties(departLimitPageParam, systemDepartLimitDto);
        StringBuilder wayUuid = new StringBuilder();
        StringBuilder wayName = new StringBuilder();
        for (Map map : departLimitPageParam.getWayInfo()) {
            wayUuid.append(",").append(map.get("limitWayUuid"));
            wayName.append(",").append(map.get("limitWayName"));
        }
        systemDepartLimitDto.setLimitWayUuid(wayUuid.toString().substring(1));
        systemDepartLimitDto.setLimitWayName(wayName.toString().substring(1));
        //编辑
        if (StringUtils.isNotEmpty(departLimitPageParam.getUuid())) {
            systemDepartLimitDto.setUuid(departLimitPageParam.getUuid());
            systemDepartLimitDto.setUpdateBy(departLimitPageParam.getUserUuid());
            systemDepartLimitDto.setUpdateOn(new Date());
            return sysDepartLimitService.edit(systemDepartLimitDto);
        } else {
            //新增
            systemDepartLimitDto.setUuid(StringUtils.buildUUID());
            systemDepartLimitDto.setCreateBy(departLimitPageParam.getUserUuid());
            systemDepartLimitDto.setCreateOn(new Date());
            systemDepartLimitDto.setUpdateBy(departLimitPageParam.getUserUuid());
            systemDepartLimitDto.setUpdateOn(new Date());
            return sysDepartLimitService.add(systemDepartLimitDto);
        }
    }

    /**
     * 新增或者编辑
     * @param departLimitPageParam
     * @return
     */
    @RequestMapping("/addOrEdit")
    @ApiOperation(value = "编辑", httpMethod = "POST")
    public ResponseData edit(@RequestBody AdminDepartLimitPageParam departLimitPageParam){
        return sysDepartLimitService.addOrEdit(departLimitPageParam);
    }

    @RequestMapping("/delete")
    @ApiOperation(value = "删除操作", httpMethod = "POST")
    public ResponseData delete(@RequestBody AdminDepartLimitPageParam departLimitPageParam) {
        SysDepartLimitDto systemDepartLimitDto = new SysDepartLimitDto();
        systemDepartLimitDto.setUuid(departLimitPageParam.getUuid());
        return sysDepartLimitService.del(systemDepartLimitDto);
    }
}
